<!doctype html>
<html>
<head>
    <!--Setting-->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta name="apple-mobile-web-app-capable"  content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no,email=no">
    
    
    <!--Simple SEO-->


<meta name="robots" content=all />
<meta name="google" content=all />
<meta name="googlebot" content=all />
<meta name="verify" content=all />
    <!--Title-->

<title>docker swarm | MIAbon&#39;s blog</title>

<link rel="alternate" href="/atom.xml" title="MIAbon&#39;s blog" type="application/atom+xml">


<link rel="icon" href="/favicon.ico">

    
<link rel="stylesheet" href="/css/base.css">
<link rel="stylesheet" href="/css/pages/post.css">
<link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="/css/thirdParty/highlight/github.css">
<link rel="stylesheet" href="/.css">

    <!--script-->


<script src="http://cdn1.lncld.net/static/js/3.2.1/av-min.js"></script>
<script>
  var appId = "i7AHmC7NPbPtgS3YxT67dRIc-9Nh9j0Va";
  var appKey = "g6TKbY8O4TsCDMcemoC3STvQ";
  var region = "";
  AV.init({
    appId: appId,
    appKey: appKey,
    region: region
  });
</script>


<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<!--<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>-->


    
    
</head>

<body id="normal">
<!--[if lte IE 8]>
<style>
    html{ font-size: 1em }
</style>
<![endif]-->
<!--[if lte IE 9]>
<style>
    header{ top: 71px; position: absolute!important;}
    #container{padding-top: 151px!important;}
</style>
<div style="position:fixed;z-index:9999;left:0;top:0;width:100%;height:70px;background-color:#e0e0e0;color:#396CA5;border-bottom:1px solid #cecece;text-align:center;line-height:70px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis">你使用的浏览器版本过低，为了你更好的阅读体验，请更新浏览器的版本或者使用其他现代浏览器，比如Chrome、Firefox、Safari等。</div>
<![endif]-->

<div id="wrap">
    <header  style="position: absolute;" >
    <div id="site-meta">
        <a href="/" id="logo">
            <h1 class="title">MIAbon&#39;s blog</h1>
        </a>
        
    </div>
    <ul id="nav">
        
            <li><a href="/"><i class="fa fa-home"></i>首页</a></li>
        
            <li><a href="/atom.xml"><i class="fa fa-rss"></i>RSS</a></li>
        
        <li id="search"><a href="javascript:void(0)"><i class="fa fa-search"></i>搜索</a></li>
    </ul>
</header>

    <div id="container">
        
<ul id="sidebar">
    
    
    
    
<li class="widget widget-normal category">
    <h3 class="fa fa-th widget-title">分类</h3>
    <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/ELK/"><i class="fa" aria-hidden="true">ELK</i></a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Lvs/"><i class="fa" aria-hidden="true">Lvs</i></a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/admin/"><i class="fa" aria-hidden="true">admin</i></a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/ansible/"><i class="fa" aria-hidden="true">ansible</i></a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/aws/"><i class="fa" aria-hidden="true">aws</i></a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/docker/"><i class="fa" aria-hidden="true">docker</i></a><span class="category-list-count">7</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/drone/"><i class="fa" aria-hidden="true">drone</i></a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/kernel/"><i class="fa" aria-hidden="true">kernel</i></a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/kubernetes/"><i class="fa" aria-hidden="true">kubernetes</i></a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/kvm/"><i class="fa" aria-hidden="true">kvm</i></a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/mongodb/"><i class="fa" aria-hidden="true">mongodb</i></a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link current" href="/categories/python/"><i class="fa" aria-hidden="true">python</i></a><span class="category-list-count">9</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/share/"><i class="fa" aria-hidden="true">share</i></a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/web/"><i class="fa" aria-hidden="true">web</i></a><span class="category-list-count">12</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/zabbix/"><i class="fa" aria-hidden="true">zabbix</i></a><span class="category-list-count">3</span></li></ul>
</li>


    
    
<li class="widget widget-normal archive">
  <h3 class="fa fa-archive widget-title">归档</h3>
    <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/"><i class="fa" aria-hidden="true">二月 2018</i></a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/01/"><i class="fa" aria-hidden="true">一月 2018</i></a><span class="archive-list-count">8</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/12/"><i class="fa" aria-hidden="true">十二月 2017</i></a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/11/"><i class="fa" aria-hidden="true">十一月 2017</i></a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/10/"><i class="fa" aria-hidden="true">十月 2017</i></a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/09/"><i class="fa" aria-hidden="true">九月 2017</i></a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/08/"><i class="fa" aria-hidden="true">八月 2017</i></a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/05/"><i class="fa" aria-hidden="true">五月 2017</i></a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/04/"><i class="fa" aria-hidden="true">四月 2017</i></a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/03/"><i class="fa" aria-hidden="true">三月 2017</i></a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/01/"><i class="fa" aria-hidden="true">一月 2017</i></a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/12/"><i class="fa" aria-hidden="true">十二月 2016</i></a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/11/"><i class="fa" aria-hidden="true">十一月 2016</i></a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/10/"><i class="fa" aria-hidden="true">十月 2016</i></a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/08/"><i class="fa" aria-hidden="true">八月 2016</i></a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/05/"><i class="fa" aria-hidden="true">五月 2016</i></a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/04/"><i class="fa" aria-hidden="true">四月 2016</i></a><span class="archive-list-count">8</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/05/"><i class="fa" aria-hidden="true">五月 2015</i></a><span class="archive-list-count">1</span></li></ul>
</li>


    
    
<li class="widget widget-normal tags">
  <h3 class="fa fa-tags widget-title">标签云</h3>
  <div class="tagcloud-content">
    
      <a href="/tags/drone/" style="font-size: 0.14rem; color: #69c">drone</a> <a href="/tags/linux/" style="font-size: 0.2rem; color: #0a407c">linux</a> <a href="/tags/kernel/" style="font-size: 0.16rem; color: #4f83b8">kernel</a> <a href="/tags/ELK/" style="font-size: 0.14rem; color: #69c">ELK</a> <a href="/tags/ansible/" style="font-size: 0.14rem; color: #69c">ansible</a> <a href="/tags/swarm/" style="font-size: 0.14rem; color: #69c">swarm</a> <a href="/tags/docker/" style="font-size: 0.18rem; color: #215690">docker</a> <a href="/tags/firewalld/" style="font-size: 0.14rem; color: #69c">firewalld</a> <a href="/tags/zabbix/" style="font-size: 0.16rem; color: #4f83b8">zabbix</a> <a href="/tags/aws/" style="font-size: 0.14rem; color: #69c">aws</a> <a href="/tags/web/" style="font-size: 0.16rem; color: #4f83b8">web</a> <a href="/tags/python/" style="font-size: 0.18rem; color: #215690">python</a> <a href="/tags/spider/" style="font-size: 0.17rem; color: #386da4">spider</a>
  </div>
</li>


    
    
<li class="widget widget-normal friends-link">
    <h3 class="fa fa-globe widget-title">友链</h3><br/>

    
        <a href="http://zhengwei666.wang" class="fa" target="_blank">zhengwei</a>

    
        <a href="https://you-deng.github.io" class="fa" target="_blank">dengyou</a>

    
        <a href="http://www.systemd.cn" class="fa" target="_blank">langyaoliang</a>

    
        <a href="http://www.chen-hao.com.cn/" class="fa" target="_blank">chenhao</a>

    
        <a href="http://www.yulongjun.com" class="fa" target="_blank">yulongjun</a>

    

</li>

    
</ul>


        <div id="main">
    <article id="post">
        <div id="post-header">

            <h1 id="docker swarm">
                
                docker swarm
                
            </h1>
            <div class="article-meta">
    
    
    <span class="categories-meta fa-wrap">
            <i class="fa fa-folder-open-o"></i>
        <span>docker</span>
    </span>
    
    
    <span class="fa-wrap">
         <i class="fa fa-tags"></i>
        <span class="tags-meta">
            
            linux
            
        </span>
    </span>
    
    
    <span class="fa-wrap">
        <i class="fa fa-clock-o"></i>
        <span class="date-meta ">2017/08/30</span>
    </span>
    
    
    <span class="fa-wrap">
            <i class="fa fa-thermometer-three-quarters"></i>
        <span class="hits hits-meta " data-leadcloud-title="docker swarm"
              data-leadcloud-url="/2017/08/30/docker_swarm/"><i class="fa fa-spinner fa-spin"></i></span>
    </span>
    
    
</div>

            
            
        </div>
        
        <div id="post-body">
            <h1 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h1><p>swarm集群测试需要3台linux主机，Docker Engine≥1.12<br>一台manager,二台worker</p>
<h1 id="打开主机之间的协议和端口"><a href="#打开主机之间的协议和端口" class="headerlink" title="打开主机之间的协议和端口"></a>打开主机之间的协议和端口</h1><p>TCP端口2377用于管理集群通信<br>TCP和UDP端口7946用于节点之间的通信<br>UDP端口4789用于overlay网络通信</p>
<h1 id="创建一个群租"><a href="#创建一个群租" class="headerlink" title="创建一个群租"></a>创建一个群租</h1><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">docker swarm init --advertise-addr &lt;MANAGER-IP&gt;</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">Swarm initialized: current node (lo7osxc5p7u6ys2eliiehwi8l) is now a manager.</div><div class="line">To add a worker to this swarm, run the following <span class="built_in">command</span>:</div><div class="line">docker swarm join \</div><div class="line">    --token SWMTKN-1-0rawcsshtnw487ed5mwiipqosyh68yhicldt3n7nf59mktn1aj-7dv4xk27dmf1zogcan0waxlf1 \</div><div class="line">    192.168.2.13:2377</div><div class="line">To add a manager to this swarm, run <span class="string">'docker swarm join-token manager'</span> and follow the instructions.</div></pre></td></tr></table></figure>
<p>–advertise-addr将管理器节点配置在192.168.2.13。集群中的其他节点必须能够访问该节点</p>
<h2 id="查看节点状态"><a href="#查看节点状态" class="headerlink" title="查看节点状态"></a>查看节点状态</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">docker node ls</div></pre></td></tr></table></figure>
<h1 id="将节点添加到集群"><a href="#将节点添加到集群" class="headerlink" title="将节点添加到集群"></a>将节点添加到集群</h1><p>如果不知道token，可以通过命令查看<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">docker swarm join-token worker</div></pre></td></tr></table></figure></p>
<p>添加node节点后可以在manager查看<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@managers ~]<span class="comment"># docker node ls</span></div><div class="line">ID                               HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS</div><div class="line">lo7osxc5p7u6ys2eliiehwi8l *   managers.tthr.com   Ready               Active              Leader</div><div class="line">xmc2mupqqnbk0r3575e8vyilj     node2.tthr.com      Ready               Active              </div><div class="line">xu1vavnbb5w0solx0i0q82t1b     node1.tthr.com      Ready               Active</div></pre></td></tr></table></figure></p>
<h1 id="将服务部署到集群"><a href="#将服务部署到集群" class="headerlink" title="将服务部署到集群"></a>将服务部署到集群</h1><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">docker service create --replicas 1 --name helloworld alpine ping docker.com</div><div class="line">docker service create命令创建服务。</div><div class="line">--name标志命名该服​​务helloworld。</div><div class="line">--replicas标志指定1个运行实例的所需状态。</div><div class="line">参数alpine ping docker.com将服务定义为执行该命令的Alpine Linux容器ping docker.com。</div></pre></td></tr></table></figure>
<h2 id="查看正在运行的服务列表"><a href="#查看正在运行的服务列表" class="headerlink" title="查看正在运行的服务列表"></a>查看正在运行的服务列表</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">docker service ls</div></pre></td></tr></table></figure>
<h1 id="检查集群服务"><a href="#检查集群服务" class="headerlink" title="检查集群服务"></a>检查集群服务</h1><p>运行docker service inspect –pretty <service-id>以易于阅读的格式显示有关服务的详细信息。<br>要以json格式返回服务详细信息，请运行不带–pretty标志的相同命令。<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line">[root@managers ~]<span class="comment"># docker service inspect --pretty helloworld</span></div><div class="line">ID:		p59xf5twjhisa3kkejgwcn181</div><div class="line">Name:		helloworld</div><div class="line">Service Mode:	Replicated</div><div class="line"> Replicas:	1</div><div class="line">Placement:</div><div class="line">UpdateConfig:</div><div class="line"> Parallelism:	1</div><div class="line"> On failure:	pause</div><div class="line"> Monitoring Period: 5s</div><div class="line"> Max failure ratio: 0</div><div class="line"> Update order:      stop-first</div><div class="line">RollbackConfig:</div><div class="line"> Parallelism:	1</div><div class="line"> On failure:	pause</div><div class="line"> Monitoring Period: 5s</div><div class="line"> Max failure ratio: 0</div><div class="line"> Rollback order:    stop-first</div><div class="line">ContainerSpec:</div><div class="line"> Image:		alpine:latest@sha256:1072e499f3f655a032e88542330cf75b02e7bdf673278f701d7ba61629ee3ebe</div><div class="line"> Args:		ping docker.com </div><div class="line">Resources:</div><div class="line">Endpoint Mode:	vip</div></pre></td></tr></table></figure></service-id></p>
<p>运行docker service ps <service-id>查看哪些节点正在运行服务<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@managers ~]<span class="comment"># docker service ps helloworld</span></div><div class="line">ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE           ERROR               PORTS</div><div class="line">iilsbhjm4zid        helloworld.1        alpine:latest       managers.tthr.com   Running             Running 5 minutes ago</div></pre></td></tr></table></figure></service-id></p>
<p>在这种情况下，服务的一个实例helloworld在managers节点上运行 。您可能会看到您的node节点上运行的服务。默认情况下，群集中的节点可以像工作节点一样执行任务。</p>
<h1 id="缩放集群中的服务"><a href="#缩放集群中的服务" class="headerlink" title="缩放集群中的服务"></a>缩放集群中的服务</h1><p>将服务部署到集群中，就可以使用docker cli来缩放服务中的容器数量。<br>在服务中运行的容器称为“任务”。<br>运行以下命令来更改在群集中运行的服务的所需状态：<br>docker service scale <service-id>=<number-of-tasks><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@managers ~]<span class="comment"># docker service scale helloworld=3</span></div><div class="line">helloworld scaled to 5</div></pre></td></tr></table></figure></number-of-tasks></service-id></p>
<p>运行docker service ps <service-id>以查看更新的任务列表：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@managers ~]<span class="comment"># docker service ps helloworld </span></div><div class="line">ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS</div><div class="line">iilsbhjm4zid        helloworld.1        alpine:latest       managers.tthr.com   Running             Running 18 minutes ago                       </div><div class="line">msmgctg5w31z        helloworld.2        alpine:latest       node1.tthr.com      Running             Running 2 minutes ago                        </div><div class="line">fjhqo4fagr0f        helloworld.3        alpine:latest       node2.tthr.com      Running             Running 2 minutes ago                        </div><div class="line">q1mz1fdpv6nc        helloworld.4        alpine:latest       node1.tthr.com      Running             Running 29 seconds ago                       </div><div class="line">m7ujox3c69c2        helloworld.5        alpine:latest       managers.tthr.com   Running             Running 2 seconds ago</div></pre></td></tr></table></figure></service-id></p>
<p>群组已经创建了4个新任务来扩展到Alpine Linux的5个运行实例。任务分布在群组的三个节点之间。两个正在运行manager<br>运行docker ps以查看在您连接的节点上运行的容器。<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@managers ~]<span class="comment"># docker ps </span></div><div class="line">CONTAINER ID        IMAGE                                          COMMAND             CREATED             STATUS              PORTS                                                                      NAMES</div><div class="line">2dbd693e9170        alpine:latest                                  <span class="string">"ping docker.com"</span>   13 seconds ago      Up 12 seconds                                                                                  helloworld.5.m7ujox3c69c20qn2yi4gh1uq8</div><div class="line">5973daeefbc6        alpine:latest                                  <span class="string">"ping docker.com"</span>   19 minutes ago      Up 19 minutes                                                                                  helloworld.1.iilsbhjm4zidncrrsqswxi8dc</div></pre></td></tr></table></figure></p>
<h1 id="删除集群上运行的服务"><a href="#删除集群上运行的服务" class="headerlink" title="删除集群上运行的服务"></a>删除集群上运行的服务</h1><p>运行docker service rm helloworld删除helloworld服务。<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">$ docker service rm helloworld</div><div class="line">helloworld</div></pre></td></tr></table></figure></p>
<p>运行docker service inspect <service-id>以验证swarm manager是否删除了该服务。CLI返回一条消息，指出没有找到该服务：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">$ docker service inspect helloworld</div><div class="line">[]</div><div class="line">Error: no such service: helloworld</div></pre></td></tr></table></figure></service-id></p>
<p>即使服务不再存在，任务容器需要几秒钟的时间来清理。您可以使用docker ps节点来验证任务何时被删除。<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">$ docker ps</div><div class="line">    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES</div><div class="line">    db1651f50347        alpine:latest       <span class="string">"ping docker.com"</span>        44 minutes ago      Up 46 seconds                           helloworld.5.9lkmos2beppihw95vdwxy1j3w</div><div class="line">    43bf6e532a92        alpine:latest       <span class="string">"ping docker.com"</span>        44 minutes ago      Up 46 seconds                           helloworld.3.a71i8rp6fua79ad43ycocl4t2</div><div class="line">    5a0fb65d8fa7        alpine:latest       <span class="string">"ping docker.com"</span>        44 minutes ago      Up 45 seconds                           helloworld.2.2jpgensh7d935qdc857pxulfr</div><div class="line">    afb0ba67076f        alpine:latest       <span class="string">"ping docker.com"</span>        44 minutes ago      Up 46 seconds                           helloworld.4.1c47o7tluz7drve4vkm2m5olx</div><div class="line">    688172d3bfaa        alpine:latest       <span class="string">"ping docker.com"</span>        45 minutes ago      Up About a minute                       helloworld.1.74nbhb3fhud8jfrhigd7s29we</div><div class="line">$ docker ps</div><div class="line">   CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS</div></pre></td></tr></table></figure></p>
<h1 id="滚动更新容器"><a href="#滚动更新容器" class="headerlink" title="滚动更新容器"></a>滚动更新容器</h1><p>将Redis 3.0.6部署到群集，并配置群组10秒更新延迟：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">$ docker service create \</div><div class="line">  --replicas 3 \</div><div class="line">  --name redis \</div><div class="line">  --update-delay 10s \</div><div class="line">  redis:3.0.6</div><div class="line">0u6a4s31ybk7yw2wyvtikmu50</div></pre></td></tr></table></figure></p>
<p>您可以在服务部署时配置滚动更新策略。</p>
<p>–update-delay用来配置服务任务或任务集更新之间的时间延迟。时间描述T为秒Ts，分钟Tm或小时数的组合Th。所以 10m30s表示10分30秒的延迟。</p>
<p>默认情况下，调度程序一次更新1个任务。您可以传递该 –update-parallelism标志来配置调度程序同时更新的最大服务任务数。</p>
<p>默认情况下，当单个任务的更新返回一个状态时 RUNNING，调度程序将调度另一个任务以更新，直到所有任务更新为止。如果在更新过程中的任何时间任务返回FAILED，则调度程序将暂停更新。您可以使用或 的–update-failure-action标志来控制行为 。docker service createdocker service update</p>
<p>检查redis服务：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">$ docker service inspect --pretty redis</div><div class="line">ID:             0u6a4s31ybk7yw2wyvtikmu50</div><div class="line">Name:           redis</div><div class="line">Service Mode:   Replicated</div><div class="line"> Replicas:      3</div><div class="line">Placement:</div><div class="line"> Strategy:	    Spread</div><div class="line">UpdateConfig:</div><div class="line"> Parallelism:   1</div><div class="line"> Delay:         10s</div><div class="line">ContainerSpec:</div><div class="line"> Image:         redis:3.0.6</div><div class="line">Resources:</div><div class="line">Endpoint Mode:  vip</div></pre></td></tr></table></figure></p>
<p>现在可以更新容器redis。manager根据UpdateConfig策略将更新应用于节点：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">$ docker service update --image redis:3.0.7 redis</div><div class="line">redis</div></pre></td></tr></table></figure></p>
<p>默认情况下，程序滚动更新如下：</p>
<p>停止第一个任务<br>更新已停止的任务。<br>启动更新完成的任务容器。<br>如果任务的更新返回RUNNING，等待指定的延迟时间，然后启动下一个任务。<br>如果在更新期间的任何时间任务返回FAILED，则暂停更新。<br>运行docker service inspect –pretty redis以查看新状态：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">$ docker service inspect --pretty redis</div><div class="line">ID:             0u6a4s31ybk7yw2wyvtikmu50</div><div class="line">Name:           redis</div><div class="line">Service Mode:   Replicated</div><div class="line"> Replicas:      3</div><div class="line">Placement:</div><div class="line"> Strategy:	    Spread</div><div class="line">UpdateConfig:</div><div class="line"> Parallelism:   1</div><div class="line"> Delay:         10s</div><div class="line">ContainerSpec:</div><div class="line"> Image:         redis:3.0.7</div><div class="line">Resources:</div><div class="line">Endpoint Mode:  vip</div></pre></td></tr></table></figure></p>
<p>service inspect显示如果更新由于失败而暂停的输出：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">$ docker service inspect --pretty redis</div><div class="line">ID:             0u6a4s31ybk7yw2wyvtikmu50</div><div class="line">Name:           redis</div><div class="line">...snip...</div><div class="line">Update status:</div><div class="line"> State:      paused</div><div class="line"> Started:    11 seconds ago</div><div class="line"> Message:    update paused due to failure or early termination of task 9p7ith557h8ndf0ui9s0q951b</div><div class="line">...snip...</div></pre></td></tr></table></figure></p>
<p>要重新启动暂停的更新，运行docker service update <service-id>。例如：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">docker service update redis</div></pre></td></tr></table></figure></service-id></p>
<p>为了避免重复某些更新故障，您可能需要通过传递标志来重新配置服务docker service update。</p>
<p>运行docker service ps <service-id>观看滚动更新：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">$ docker service ps redis</div><div class="line">NAME                                   IMAGE        NODE       DESIRED STATE  CURRENT STATE            ERROR</div><div class="line">redis.1.dos1zffgeofhagnve8w864fco      redis:3.0.7  worker1    Running        Running 37 seconds</div><div class="line"> \_ redis.1.88rdo6pa52ki8oqx6dogf04fh  redis:3.0.6  worker2    Shutdown       Shutdown 56 seconds ago</div><div class="line">redis.2.9l3i4j85517skba5o7tn5m8g0      redis:3.0.7  worker2    Running        Running About a minute</div><div class="line"> \_ redis.2.66k185wilg8ele7ntu8f6nj6i  redis:3.0.6  worker1    Shutdown       Shutdown 2 minutes ago</div><div class="line">redis.3.egiuiqpzrdbxks3wxgn8qib1g      redis:3.0.7  worker1    Running        Running 48 seconds</div><div class="line"> \_ redis.3.ctzktfddb2tepkr45qcmqln04  redis:3.0.6  mmanager1  Shutdown       Shutdown 2 minutes ago</div></pre></td></tr></table></figure></service-id></p>
<p>在Swarm更新所有任务之前，您可以看到有些正在运行， redis:3.0.6而其他任务正在运行redis:3.0.7。上述输出显示滚动更新完成后的状态。</p>
<h1 id="排除集群上的部署节点"><a href="#排除集群上的部署节点" class="headerlink" title="排除集群上的部署节点"></a>排除集群上的部署节点</h1><p>启动redis服务<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">docker service create --replicas 3 --name redis --update-delay 10s redis:3.0.6</div></pre></td></tr></table></figure></p>
<p>运行docker service ps redis以查看manager如何将任务分配给不同的节点：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@managers ~]<span class="comment"># docker service ps redis</span></div><div class="line">ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE                  ERROR               PORTS</div><div class="line">twsp4215knyo        redis.1             redis:3.0.6         node2.tthr.com      Running             Running about a minute ago                         </div><div class="line">z56r3bybivm6        redis.2             redis:3.0.6         managers.tthr.com   Running             Running 46 seconds ago                             </div><div class="line">qg9dyknjkpz3        redis.3             redis:3.0.6         node1.tthr.com      Running             Preparing about a minute ago                       </div><div class="line">cho79ccepng7        redis.4             redis:3.0.6         node2.tthr.com      Running             Running about a minute ago                         </div><div class="line">ltck2yzgybky        redis.5             redis:3.0.6         managers.tthr.com   Running             Running 46 seconds ago</div></pre></td></tr></table></figure></p>
<p>运行docker node update –availability drain <node-id>以排除分配给任务的节点：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#运行docker node update --availability drain &lt;NODE-ID&gt;以排除分配给任务的节点：</span></div><div class="line">[root@managers ~]<span class="comment"># docker node update --availability drain node2.tthr.com</span></div><div class="line">node2.tthr.com</div><div class="line"><span class="comment">#检查节点检查其可用性：</span></div><div class="line">[root@managers ~]<span class="comment"># docker node inspect --pretty node2.tthr.com </span></div><div class="line">ID:			xmc2mupqqnbk0r3575e8vyilj</div><div class="line">Hostname:              	node2.tthr.com</div><div class="line">Joined at:             	2017-08-23 04:08:50.957822265 +0000 utc</div><div class="line">Status:</div><div class="line"> State:			Ready</div><div class="line"> Availability:         	Drain</div><div class="line"> Address:		192.168.2.15</div><div class="line">Platform:</div><div class="line"> Operating System:	linux</div><div class="line"> Architecture:		x86_64</div><div class="line">Resources:</div><div class="line"> CPUs:			2</div><div class="line"> Memory:		1.953GiB</div><div class="line">Plugins:</div><div class="line"> Network:		bridge, host, macvlan, null, overlay</div><div class="line"> Volume:		<span class="built_in">local</span></div><div class="line">Engine Version:		17.05.0-ce</div><div class="line"><span class="comment">#运行docker service ps redis以查看manager如何更新服务的任务分配redis：</span></div><div class="line">[root@managers ~]<span class="comment"># docker service ps redis</span></div><div class="line">ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE                 ERROR               PORTS</div><div class="line">8y6ewgnk54kd        redis.1             redis:3.0.6         node1.tthr.com      Running             Running about a minute ago                        </div><div class="line">twsp4215knyo         \_ redis.1         redis:3.0.6         node2.tthr.com      Shutdown            Shutdown about a minute ago                       </div><div class="line">z56r3bybivm6        redis.2             redis:3.0.6         managers.tthr.com   Running             Running 3 minutes ago                             </div><div class="line">qg9dyknjkpz3        redis.3             redis:3.0.6         node1.tthr.com      Running             Running 2 minutes ago                             </div><div class="line">4p8l97el713r        redis.4             redis:3.0.6         node1.tthr.com      Running             Running about a minute ago                        </div><div class="line">cho79ccepng7         \_ redis.4         redis:3.0.6         node2.tthr.com      Shutdown            Shutdown about a minute ago                       </div><div class="line">ltck2yzgybky        redis.5             redis:3.0.6         managers.tthr.com   Running             Running 3 minutes ago</div></pre></td></tr></table></figure></node-id></p>
<h1 id="发布服务端口"><a href="#发布服务端口" class="headerlink" title="发布服务端口"></a>发布服务端口</h1>
        </div>
        <div id="post-footer">
            <div class="avatar" >
                <img src="/img/author.jpg" alt="avatar"/>
                <!-- 
                <a href="javascript:void(0)" class="high-song">high起来 &#128541;</a>
                 -->
                
                <a href="http://backup-miabon.oss-cn-hongkong.aliyuncs.com/img/alipay.jpg" target="_blank" class="donate fa">打赏小弟 &#128536;</a>
                
            </div>
            <ul class="author-profile-section">
                <li>
                  
                  作者:
                  
                    
                    <a href="/about.html">MIAbon</a>
                </li>
                
                <li>发表日期: <span>2017-08-30  00:00:00</span></li>
                
                <li>最后编辑日期: <span>2017-11-23  08:23:00</span></li>
                
                <li class="post-category">
                    文章分类:
                    
                    <a href="/categories/docker/">docker</a>
                    
                </li>
                <li class="post-tags">
                    文章标签:
                    
                    <a href="/tags/linux/">linux</a>
                    
                    <a href="/tags/docker/">docker</a>
                    
                    <a href="/tags/swarm/">swarm</a>
                    
                </li>
                
                <li> 版权声明: <a href="https://creativecommons.org/licenses/by-nc-nd/3.0/" target="_blank">
知识共享署名-非商业性使用-禁止演绎 3.0 未本地化版本许可协议（CC BY-NC-ND 3.0）
</a></li>
                
            </ul>
            <div id="donate-wrap">
                
                
                
                <img src="http://www.geasslinks.com/img/alipay.jpg" alt="支付宝付款" class="donate-img">
                
                
            </div>
        </div>
    </article>
    <div class="article-nav">
        
        <a href="/2017/09/01/Docker_Remore_API的使用范例/" class="pre-post fa fa-caret-left">docker_remote_API</a>
        
        
        <a href="/2017/05/24/firewalld/" class="next-post fa">firewalld示例</a>
        
    </div>
    
    <div id="comments">
        

<script>
  gitment.render(document.getElementById("comments"));
</script>



    </div>
    
</div>


    </div>
    <footer id="footer">
    
    <div class="social">
        
        <a href="https://www.example1.com" class="fa fa-free-code-camp" target="_blank" title="freecodecamp"></a>
        
        <a href="https://github.com/werewolf2101" class="fa fa-github" target="_blank" title="Follow me~"></a>
        
        <a href="mailto:werewolf2101@gmail.com" class="fa fa-email" target="_blank" title="Email"></a>
        
    </div>
    
    <div>
        
        <a href="/" class="copyright-links">MIAbon</a>&copy;2015 - 2018.All Rights
        Reserved.
    </div>
    <p>Powered by <a href="https://hexo.io" class="copyright-links" target="_blank">Hexo</a> | Theme by <a
                href="https://github.com/GeekaholicLin" class="copyright-links" target="_blank">GeekaholicLin</a>
    </p>
    
    
    <p>
        <span id="busuanzi_container_site_uv" class="fa fa-bar-chart">
        欢迎第<span id="busuanzi_value_site_uv"><i class="fa fa-spinner fa-spin"></i></span>位小伙伴~
        </span>
    </p>
    
</footer>

</div>
    <ul id="tools">
    <li class="totop-btn fa fa-angle-up"></li>
    <li class="exchange-btn fa fa-exchange"></li>
  
    <li class="toc-btn fa fa-list-ul"></li>
    
    

    
</ul>
<p id="process"></p>
<div id="search-overlay">
    <div class="search-area-wrap">
        <div id="search-area">
            <div class="input-wrap focus">
                <i class="fa fa-search" aria-hidden="true"></i>
                <input id="search-input" autofocus autocomplete="off" type="text"
                       placeholder="search this website..."/>
            </div>
            <ul id="search-result">
                <li class="load-first"><i class="fa fa-spinner fa-pulse"></i></li>
            </ul>
        </div>
    </div>
</div>

    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#简介"><span class="toc-number">1.</span> <span class="toc-text">简介</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#打开主机之间的协议和端口"><span class="toc-number">2.</span> <span class="toc-text">打开主机之间的协议和端口</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#创建一个群租"><span class="toc-number">3.</span> <span class="toc-text">创建一个群租</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#查看节点状态"><span class="toc-number">3.1.</span> <span class="toc-text">查看节点状态</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#将节点添加到集群"><span class="toc-number">4.</span> <span class="toc-text">将节点添加到集群</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#将服务部署到集群"><span class="toc-number">5.</span> <span class="toc-text">将服务部署到集群</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#查看正在运行的服务列表"><span class="toc-number">5.1.</span> <span class="toc-text">查看正在运行的服务列表</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#检查集群服务"><span class="toc-number">6.</span> <span class="toc-text">检查集群服务</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#缩放集群中的服务"><span class="toc-number">7.</span> <span class="toc-text">缩放集群中的服务</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#删除集群上运行的服务"><span class="toc-number">8.</span> <span class="toc-text">删除集群上运行的服务</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#滚动更新容器"><span class="toc-number">9.</span> <span class="toc-text">滚动更新容器</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#排除集群上的部署节点"><span class="toc-number">10.</span> <span class="toc-text">排除集群上的部署节点</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#发布服务端口"><span class="toc-number">11.</span> <span class="toc-text">发布服务端口</span></a></li></ol>


    <script src="/js/highsong.js"></script>



<script src="/js/search.js"></script>
<script type="text/javascript">
    //theme config datas
    var copyrightObj = {};
    copyrightObj.enable = 'true';
    copyrightObj.triggerCopyLength = '200';
    copyrightObj.appendText = '商业转载请联系作者获得授权,非商业转载请注明出处 © gasslinks.com';
    var leancloudObj = {};
    leancloudObj.enable = 'true';
    leancloudObj.className = 'blog';
    leancloudObj.limits = '5';
</script>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?dfebe5842f5d4196471b84802392a4b2";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>
<script type="text/javascript">
    var search = {};
    var search_path = "search.xml";
    if (!search_path) {
        search_path = "search.xml";
    }
    search.path = "/" + search_path;
    search.func =  _ajax.init();
</script>
<script src="/js/app.js"></script>


</body>
</html>